<template>
    <div>
        <div style="display:flex;margin-bottom:20px">
            <el-input style="width:200px" placeholder="按日期查询(YYYY-MM-DD)" v-model="shoptime"></el-input>
            <el-input style="width:200px;margin:0 5px" placeholder="ISBN查询" v-model="isbn"></el-input>
            <el-button type="primary" @click="load(1)">查询</el-button>
            <el-button type="info" @click="reset" style="background-color:rgb(182, 233, 15);border:none">重置</el-button>
        </div>
        <el-table :data="tableData" stripe :header-cell-style="{backgroundColor:'aliceblue',color:'#666'}" @selection-change="handleSelectionChange">
            <el-table-column
            type="index"
            width="50">
            </el-table-column>
            <el-table-column prop="isbn"  label="ISBN"></el-table-column>
            <el-table-column prop="shoptime" label="日期"></el-table-column>
            <el-table-column prop="bprice" label="进价"></el-table-column>
            <el-table-column label="操作" align="center" width="180">
                <template v-slot="scope">
                    <el-button size="mini" type="danger" plain @click="handleEdit(scope.row)">取消订单</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin:10px 0;">
            <el-pagination
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default{
    name:"Buy",
    data(){
        return {
            tableData:[],
            pageNum:1,
            pageSize:5,
            shoptime:'',
            isbn:'',
            total:0,
            space:{},
            book:{},
            user:JSON.parse(localStorage.getItem('honey-user') || '{}'),
            orderids:[],

        }
    },
    created(){
        this.load()
    },
    methods:{
        handleEdit(row){
            this.space=JSON.parse(JSON.stringify(row))//给space对象赋值,注意要深拷贝
            this.$confirm('您确认取消订单吗？', '确认取消', {type: "warning"}).then(response => {
                this.$request.get('/book/selectByIsbn/'+this.space.isbn).then(res=>{
                    if(res.data.state==='售出'){
                        this.$message.error("书籍已售出，无法取消")
                    }else{
                        this.$request.delete('/book/delete/'+this.space.isbn).then(res => {
                            if (res.code === '200') {  // 表示成功保存
                                this.$message.success('取消成功')
                                this.load(1)
                            } else {
                                this.$message.error(res.msg)  // 弹出错误的信息
                            }
                        })
                    }
                })
            }).catch(() => {})
        },

        handleSelectionChange(rows){
            this.orderids=rows.map(v=>v.orderid)
        },
        reset(){
            this.shoptime='',
            this.isbn='',
            this.load()
        },
        load(pageNum) {  // 分页查询
        if(pageNum){
            this.pageNum=pageNum
        }
        this.$request.get('/book/selectByPage1', {
        params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            shoptime: this.shoptime,
            isbn: this.isbn,
        }
        }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        })
        },
        handleCurrentChange(pageNum) {
            this.load(pageNum)
        },
    }
}
</script>

<style scoped>

</style>